﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using Microsoft.BizTalk.Operations;
using System.Timers;
using System.Configuration;
using System.Diagnostics;

namespace BizTalkOneService
{
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, Namespace=Settings.Namespace)]
    public class BizTalkOneService : IBizTalkOneService 
    {
        EventLog appLog = new EventLog();
        Settings settings = new Settings();
        private BizTalkInstances bti = new BizTalkInstances(); 
        private BizTalkPorts btp = new BizTalkPorts(); 
        private BizTalkHosts bth = new BizTalkHosts();
        private BizTalkOrchestrations bto = new BizTalkOrchestrations();
        private FileCount fc = new FileCount();
        private FreeDiskSpace fds = new FreeDiskSpace();
        private EsbExceptionDb esb = new EsbExceptionDb(); 
        private HostThrottleStatus hts = new HostThrottleStatus();
        private SqlJobs sj = new SqlJobs();

        private List<Counter> InstanceList = new List<Counter>();
        private List<Counter> PortList = new List<Counter>();
        private List<Counter> HostList = new List<Counter>();
        private List<Counter> OrchestrationList = new List<Counter>();
        private List<Counter> FileCountList = new List<Counter>();
        private List<Counter> DiskSpaceList = new List<Counter>();
        private List<Counter> EsbExceptionDbList = new List<Counter>();
        private List<Counter> HostThrottleList = new List<Counter>();
        private List<Counter> SqlJobsList = new List<Counter>();
        private List<Counter> CounterList = new List<Counter>();
    
        public BizTalkOneService()
        {         
            appLog.Source = Settings.EventLogSource;
            appLog.WriteEntry("Starting BizTalkOne");
            CollectCounters();
            Timer aTimer = new Timer();
            aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
            aTimer.Interval = settings.interval;
            aTimer.Enabled = true;
        }

        private void OnTimedEvent(object source, ElapsedEventArgs e)
        {
            CollectCounters();
        }

        private void CollectCounters()
        {
            try
            {
                if (settings.HostInstanceThrottlingEnabled)
                {
                    this.hts = new HostThrottleStatus();
                    List<Counter> TempHostThrottleList = new List<Counter>();
                    TempHostThrottleList.AddRange(hts.getStatus());
                    this.HostThrottleList = TempHostThrottleList;
                }

                if (settings.BizTalkInstancesEnabled)
                {
                    this.bti = new BizTalkInstances();
                    List<Counter> TempInstanceList = new List<Counter>();
                    TempInstanceList.AddRange(bti.GetInstances());
                    this.InstanceList = TempInstanceList;
                }

                if (settings.BizTalkPortsEnabled)
                {
                    this.btp = new BizTalkPorts();
                    List<Counter> TempPortList = new List<Counter>();
                    TempPortList.AddRange(btp.GetReceiveLocations());
                    TempPortList.AddRange(btp.GetSendPorts());
                    this.PortList = TempPortList;
                }

                if (settings.BizTalkHostInstancesEnabled)
                {
                    this.bth = new BizTalkHosts();
                    List<Counter> TempHostList = new List<Counter>();
                    TempHostList.AddRange(bth.GetAllHosts());
                    this.HostList = TempHostList;
                }

                if (settings.BizTalkOrchestrationsEnabled)
                {
                    this.bto = new BizTalkOrchestrations();
                    List<Counter> TempOrchList = new List<Counter>();
                    TempOrchList.AddRange(bto.GetOrchestrations());
                    this.OrchestrationList = TempOrchList;
                }

                if (settings.SqlJobEnabled)
                {
                    this.sj = new SqlJobs();
                    List<Counter> TempSqlJobsList = new List<Counter>();
                    TempSqlJobsList.AddRange(sj.GetSqlJobList());
                    this.SqlJobsList = TempSqlJobsList;
                }

                if (settings.FileCountEnabled)
                {
                    this.fc = new FileCount();
                    List<Counter> TempFileCountList = new List<Counter>();
                    TempFileCountList.AddRange(fc.GetFilePaths());
                    this.FileCountList = TempFileCountList;
                }

                if (settings.DiskSpaceEnabled)
                {
                    this.fds = new FreeDiskSpace();
                    List<Counter> TempDiskSpaceList = new List<Counter>();
                    TempDiskSpaceList.AddRange(fds.GetFreeDiskSpace());
                    this.DiskSpaceList = TempDiskSpaceList;
                }

                if (settings.EsbExceptionsEnabled)
                {
                    this.esb = new EsbExceptionDb();
                    List<Counter> TempEsbExceptionDbList = new List<Counter>();
                    TempEsbExceptionDbList.AddRange(esb.GetAppList());
                    this.EsbExceptionDbList = TempEsbExceptionDbList;
                }
            }
            catch (Exception ex)
            { }
        }

        public List<Counter> GetCounterList() 
        {
            this.CounterList = new List<Counter>();
            CounterList.AddRange(this.HostThrottleList);
            CounterList.AddRange(this.HostList);
            CounterList.AddRange(this.OrchestrationList); 
            CounterList.AddRange(this.InstanceList);
            CounterList.AddRange(this.PortList);
            CounterList.AddRange(this.SqlJobsList);
            CounterList.AddRange(this.DiskSpaceList);
            CounterList.AddRange(this.FileCountList);
            CounterList.AddRange(this.EsbExceptionDbList);
            return CounterList; 
        
        }

    }
    
}
